<template>
  <div class="user-list-container">
    <el-card class="box-card">
      <!-- 搜索栏 -->
      <div class="filter-container">
        <el-input
            v-model="listQuery.name"
            placeholder="搜索用户名"
            style="width: 300px"
            clearable
            @keyup.enter="handleFilter"
        />
        <el-button type="primary" @click="handleFilter">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
      </div>

      <!-- 表格 -->
      <el-table
          :data="tableData"
          border
          fit
          highlight-current-row
          style="width: 100%"
          v-loading="listLoading"
      >
        <el-table-column prop="id" label="ID" width="220" align="center" />
        <el-table-column prop="userId" label="用户id" width="150" />
        <el-table-column prop="username" label="用户名称" width="100" />
        <el-table-column prop="mileage" label="公里数" width="100" />
        <el-table-column prop="oilVolume" label="加油量" width="100" />
        <el-table-column prop="amount" label="金额" width="100" />
        <el-table-column prop="oilPrice" label="单价" width="100" />
        <el-table-column prop="refuelDate" label="加油日期" width="200"/>
        <el-table-column prop="gasStation" label="加油站" width="200"/>
        <el-table-column prop="remark" label="备注" width="200"/>
        <el-table-column prop="createdAt" label="创建时间" width="200">
          <template #default="{ row }">
            {{ new Date(row.createdAt).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }) }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          v-model:current-page="listQuery.page"
          v-model:page-size="listQuery.pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="getList"
          @current-change="getList"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import axios from '@/utils/request'
import { ElMessage } from 'element-plus'

// 表格数据
const tableData = ref([])
const total = ref(0)
const listLoading = ref(false)

// 查询参数
const listQuery = reactive({
  page: 1,
  pageSize: 10,
  name: ''
})

// 获取列表
const getList = async () => {
  listLoading.value = true
  try {
    const response = await axios.get('/admin/record/list', {
      params: listQuery
    })

    tableData.value = response.data.list || []
    total.value = response.data.total || 0
  } catch (error) {
    console.error('获取列表失败:', error)
    ElMessage.error('获取列表失败: ' + (error.response?.data?.message || error.message))
  } finally {
    listLoading.value = false
  }
}

// 搜索
const handleFilter = () => {
  listQuery.page = 1
  getList()
}

// 初始化获取数据
onMounted(() => {
  getList()
})
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.filter-container {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
}

.box-card {
  margin-bottom: 20px;
}
</style>